package Test;

import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class TestDemo15 {
    public static void main1(String[] args) throws InterruptedException {
        ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
        new Thread(new Runnable() {
            @Override
            public void run() {
                reentrantReadWriteLock.readLock().lock();
                for (int i = 0; i < 5; i++) {
                    try {
                        System.out.println(i);
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                reentrantReadWriteLock.readLock().unlock();
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                reentrantReadWriteLock.readLock().lock();
                System.out.println(Thread.currentThread().getName()+"进来了");
                reentrantReadWriteLock.readLock().unlock();
            }
        }).start();

    }
    public static void main4(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
        thread.join();
        System.out.println("结束");
    }
    public static void main3(String[] args) {
        Thread t1 = new Thread(() -> {
            System.out.println("Thread 1 start");
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread 1 end");
        });

        Thread t2 = new Thread(() -> {
            System.out.println("Thread 2 start");
            System.out.println("Thread 2 end");
        });

        t1.start();
        t2.start();
    }
    public static void main2(String[] args) throws InterruptedException, ExecutionException {
        FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return 100;
            }
        });
        Thread thread = new Thread(task);
        thread.start();
        TimeUnit.SECONDS.sleep(2);

    }
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(20, new Runnable() {
            @Override
            public void run() {
                System.out.println("发车");
            }
        });
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println("加1");
                        cyclicBarrier.await();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
